﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using DMS.Business.Helpers;
using DMS.Dal;
using DMS.Entities;

namespace DMS.Business
{
    public static class ProductService
    {
        /// <summary>
        /// tuan.tran
        /// Lay thong tin tat ca san pham
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public static List<Product> GetAll(long id, string name, int pageIndex, int pageSize, out int totalRecords)
        {
            using (var context = new DMSContext(IsolationLevel.ReadUncommitted))
            {
                IQueryable<Product> query = context.Products.AsNoTracking();

                if (id != 0)
                {
                    query = query.Where(x => x.ID == id);
                }

                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.Where(x => x.Name.Contains(name));
                }

                totalRecords = query.Count();
                return query.OrderBy(x => x.ID).Skip(pageSize*(pageIndex - 1)).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// tuan.tran
        /// Them moi san pham
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public static void Add(long id, string name)
        {
            try
            {
                using (var context = new DMSContext(IsolationLevel.ReadCommitted))
                {
                    var product = new Product {ID = id, Name = name};

                    context.Products.Add(product);
                    context.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                Logger.DefaultLogger.Error(string.Format("Lỗi thêm mới sản phẩm. Chi tiết: {0}",
                                                         exception.InnerException.Message));
                throw;
            }
        }

        /// <summary>
        /// tuan.tran
        /// chinh sua san pham
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public static void Edit(long id, string name)
        {
            try
            {
                using (var context = new DMSContext(IsolationLevel.ReadCommitted))
                {
                    Product product = context.Products.First(x => x.ID == id);

                    product.Name = name;
                    context.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                Logger.DefaultLogger.Error(string.Format("Lỗi chỉnh sửa sản phẩm. Chi tiết: {0}",
                                                         exception.InnerException.Message));
                throw;
            }
        }

        /// <summary>
        /// tuan.tran
        /// Xoa san pham
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(long id)
        {
            try
            {
                using (var context = new DMSContext(IsolationLevel.ReadCommitted))
                {
                    Product product = context.Products.First(x => x.ID == id);

                    context.Products.Remove(product);
                    context.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                Logger.DefaultLogger.Error(string.Format("Lỗi xóa sản phẩm. Chi tiết: {0}",
                                                         exception.InnerException.Message));
                throw;
            }
        }

        /// <summary>
        /// tuan.tran
        /// Get thong tin san pham theo id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Product GetByID(long id)
        {
            using (var context = new DMSContext(IsolationLevel.ReadUncommitted))
            {
                return context.Products.First(x => x.ID == id);
            }
        }
    }
}